import { useState } from 'react';
import { uploadFile } from '@/services/upload';

export function useUpload() {
  const [isUploading, setIsUploading] = useState(false);
  const [error, setError] = useState<Error | null>(null);

  const upload = async (file: File) => {
    if (!file) {
      throw new Error('上传需要文件');
    }

    setIsUploading(true);
    setError(null);

    try {
      const result = await uploadFile(file);
      return result;
    } catch (err) {
      console.error('上传文件失败:', err);
      setError(err instanceof Error ? err : new Error('上传失败'));
      throw err;
    } finally {
      setIsUploading(false);
    }
  };

  return {
    isUploading,
    error,
    upload,
  };
}
